package com.mjh.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import com.mjh.bean.Article;
import com.mjh.comm.DBInstance;

public class ArticleDAO {

	public static Logger log = Logger.getLogger(ArticleDAO.class);
	
	public static final String NAME_SPACE = "com.mjh.dao.mapper.article";
	
	public int insert(Article article){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return session.insert(NAME_SPACE + ".insert", article);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.commit();
			session.close();
		}
		return 0;
	}
	
	public int update(Article article){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return session.update(NAME_SPACE + ".update", article);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.commit();
			session.close();
		}
		return 0;
	}

	public int updateCount(int articleId){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return session.update(NAME_SPACE + ".increaseClick", articleId);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.commit();
			session.close();
		}
		return 0;
	}
	
	public int delete(int id){
		
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return session.update(NAME_SPACE + ".del", id);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.commit();
			session.close();
		}
		return 0;		
	}
	
	public Article selectById(int id) {
		SqlSession session = DBInstance.getInstance().getSession();
		Article article = null;
		try {
			article = (Article) session.selectOne(NAME_SPACE + ".selectByid", id);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.close();
		}
		return article;
	}

	@SuppressWarnings("unchecked")    
	public List<Article> selectForList(Map<String, Object> par){
		SqlSession session = DBInstance.getInstance().getSession();
		List<Article> articles = new ArrayList<Article>(1);
		try {
			return  session.selectList(NAME_SPACE + ".selectForList", par);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.close();
		}
		return articles;
	}
	
	public int selectCount(Map<String, Object> par){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return  (Integer)session.selectOne(NAME_SPACE + ".selectCount", par);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.close();
		}
		return 0;
	}
	
	public int recordClickNumber(int articleId){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return  (Integer)session.update(NAME_SPACE + ".recordClick", articleId);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.commit();
			session.close();
		}
		return 0;
	}
	
	@SuppressWarnings("unchecked")    
	public List<Article> selectTopClick(Map<String, Object> par){
		SqlSession session = DBInstance.getInstance().getSession();
		List<Article> articles = new ArrayList<Article>(1);
		try {
			return  session.selectList(NAME_SPACE + ".topClick", par);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.close();
		}
		return articles;
	}
	
	@SuppressWarnings("unchecked")    
	public List<Article> selectByIds(List<Integer> ids){
		SqlSession session = DBInstance.getInstance().getSession();
		List<Article> articles = new ArrayList<Article>(1);
		try {
			return  session.selectList(NAME_SPACE + ".selectByIds", ids);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.close();
		}
		return articles;
	}
	
	public int updateIndexStatus(Map<String, Object> par){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return session.update(NAME_SPACE + ".updateIndexStatus", par);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.commit();
			session.close();
		}
		return 0;
	}
	
	public static void main(String[] args) {
		ArticleDAO dao = new ArticleDAO();
		List<Integer> ids = new ArrayList<Integer>();
		ids.add(1);
		ids.add(2);
		dao.selectByIds(ids);
		//HashMap<String,Object> parameter = new HashMap<String,Object>();
		//parameter.put("category", 1000);
		//parameter.put("artType", 1000);
		//parameter.put("pageStart", 0);
		//parameter.put("pageSize", 20);
		//List<Article> l = dao.selectForList(parameter);
		//Article a = dao.selectById(1);
		//dao.update(a);
		//dao.delete(11);
	}
}
